import type {RouteRecordRaw} from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & {id?:string};

// 定义全部路由：Admin下的子一级。
const allRoutes:IRoute[] = [
    {
        id:"1",
        name:"/",
        path: "",
        redirect: "/Admin/Myhome"
    },
    {
        id:"1",
        name:"Myhome",
        path: "Myhome",
        component: () => import("../views/Admin/Myhome/home.vue")
    },
    {
        id:"2-1",
        name:"shoppylist",
        path: "shoppylist",
        component: () => import("../views/Admin/Shoppy/shoppylist.vue")
    },
    {
        id:"2-2",
        name:"shoppyye",
        path: "shoppyye",
        component: () => import("../views/Admin/Shoppy/shoppyye.vue")
    },
    {
        id:"3-1",
        name:"List",
        path: "List",
        component: () => import("../views/Admin/Banner/List.vue")
    },
    {
        id:"3-2",
        name:"Add",
        path: "Add",
        component: () => import("../views/Admin/Banner/Add.vue")
    },
    {
        id:"4-1",
        name:"renList",
        path: "renList",
        component: () => import("../views/Admin/Admin/renList.vue")
    },
    {
        id:"4-2",
        name:"renAdd",
        path: "renAdd",
        component: () => import("../views/Admin/Admin/renAdd.vue")
    },
   
    {
        id:"5-1",
        name:"commodity",
        path: "commodity",
        component: () => import("../views/Admin/commodity/commodity.vue")
    },
    {
        id:"5-2",
        name:"user",
        path: "user",
        component: () => import("../views/Admin/commodity/user.vue")
    },
    {
        id:"6-1",
        name:"dinglisy",
        path: "dinglisy",
        component: () => import("../views/Admin/dingdan/dinglisy.vue")
    },
    {
        id:"6-2",
        name:"dingxiu",
        path: "dingxiu",
        component: () => import("../views/Admin/dingdan/dingxiu.vue")
    },
    
    {
        id:"7-1",
        name:"Kxian",
        path: "Kxian",
        component: () => import("../views/Admin/Kxian/Kxian.vue")
    },
    {
        id:"7-2",
        name:"user",
        path: "user",
        component: () => import("../views/Admin/Kxian/shuju.vue")
    },
    
]      



export default function getRoutes(){
    // 1、获取当前用户的权限
    let checkedKeys:string[] = [];
    const grants =  sessionStorage.getItem("grants");
    if(grants){
        checkedKeys = JSON.parse(grants);
    }
    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','5-2','6','6-1','6-2','7','7-1','7-2',];
    }

    // 2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedKeys.includes(item.id as string)){
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            router.addRoute("Admin",{...item});
        }
    })


    console.log("router.getRoutes()",router.getRoutes());
}

export const  clearCurrentRoutes = ()=>{
   // 1、获取当前用户的权限
   let checkedKeys:string[] = [];
   const grants =  sessionStorage.getItem("grants");
   if(grants){
       checkedKeys = JSON.parse(grants);
   }
   if(checkedKeys.length===0){
       checkedKeys = ['1','2','2-1','2-2','2-3','3','3-1','3-2','4','4-1','4-2','5','5-1','5-2'];
   }

   // 2、删除当前用户的路由配置

   allRoutes.forEach(item=>{
       if(checkedKeys.includes(item.id as string)){
           // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
        //    router.addRoute("Admin",{...item});
          router.removeRoute(item.name as string);
       }
   })

}